package com.xiaoc.bulldozer.common.service.impl;

import java.util.List;
import java.util.Set;

import javax.annotation.Resource;

import com.xiaoc.bulldozer.common.Constant;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.xc.framework.common.utils.NumberUtil;
import com.xiaoc.bulldozer.common.ConfigKeys;
import com.xiaoc.bulldozer.common.dao.AwardMapper;
import com.xiaoc.bulldozer.common.dto.AwardDto;
import com.xiaoc.bulldozer.common.dto.AwardQueryDto;
import com.xiaoc.bulldozer.common.model.Award;
import com.xiaoc.bulldozer.common.service.AwardService;
import com.xiaoc.bulldozer.common.service.cache.CommonConfigCacheService;

/**
 * Award Service 实现类
 *
 * @author Danfo Yam
 * @date 2018-08-24 15:47:58
 */
@Service("awardService")
public class AwardServiceImpl implements AwardService {

    private Logger logger = LoggerFactory.getLogger(AwardServiceImpl.class);

    @Resource
    private AwardMapper awardMapper;

    @Resource(name = "commonConfigService")
    private CommonConfigCacheService commonConfigService;

    @Override
    public Integer getAwardCountByCondition(AwardQueryDto awardQueryDto) {
        return awardMapper.getAwardCountByCondition(awardQueryDto);
    }

    @Override
    public List<Award> getAwardListByCondition(AwardQueryDto awardQueryDto) {
        awardQueryDto.setPage(awardQueryDto.getPage());
        return awardMapper.getAwardListByCondition(awardQueryDto);
    }

    @Override
    public Award getAwardById(Integer id) {
        return awardMapper.getAwardById(id);
    }

    @Override
    public List<Award> getAllAwardList() {
        return awardMapper.getAllAwardList();
    }

    @Override
    public void save(Award award) {
        awardMapper.save(award);
    }

    @Override
    public void update(Award award) {
        awardMapper.update(award);
    }

    @Override
    public void deleteById(Integer id) {
        awardMapper.deleteById(id);
    }

    @Override
    public Award getAwardByUserIdAndStatus(Integer userId, Integer status) {

        return awardMapper.getAwardByUserIdAndStatus(userId, status);
    }

    @Override
    public int getMaxRoundCountByUserId(Integer userId) {
        return awardMapper.getMaxRoundCountByUserId(userId);
    }

    @Override
    public List<Award> getAwardByUserIdAndRoundCount(Integer userId, Integer roundCount) {
        if (userId == null || roundCount == null) {
            return null;
        }
        return awardMapper.getAwardByUserIdAndRoundCount(userId, roundCount);
    }

    @Override
    public Award getAwardByIdAndUserId(Integer id, Integer userId) {
        if (id == null || userId == null) {
            return null;
        }
        return awardMapper.getAwardByIdAndUserId(id, userId);
    }

    @Override
    public List<AwardDto> getRecentAwardList(Integer limit) {
        return awardMapper.getRecentAwardList(limit);
    }

    @Override
    public Award getRecentAwardByUserId(Integer userId) {
        return awardMapper.getRecentAwardByUserId(userId);
    }

    @Override
    public List<Award> getAwardListByStatus(Integer status) {
        return awardMapper.getAwardListByStatus(status);
    }

    @Override
    public void updateStatusByIds(Integer status, Set<Integer> awardIds) {

        awardMapper.updateStatusByIds(status, awardIds);
    }

    @Override
    public Long getAwardTimeout() {

        Long defaultTimeout = 86400000L;

        String awardTimeout = commonConfigService.getValue(ConfigKeys.DEFAULT_AWARD_TIME_OUT);

        try {
            if (StringUtils.isEmpty(awardTimeout) || !NumberUtil.isNumeric(awardTimeout)) {
                logger.error("get award time out failed");
                return defaultTimeout;
            }
            return Long.valueOf(awardTimeout) * 60 * 1000L;
        } catch (Exception e) {
            logger.error("get award time out failed", e);
            return defaultTimeout;
        }
    }

    @Override
    public int getAwardSuccessCountByUserIdAndRoundCount(Integer userId, Integer roundCount) {
        return awardMapper.getAwardSuccessCountByUserIdAndRoundCount(userId, roundCount, Constant.SUCCESS_DISABLE);
    }

    @Override
    public List<AwardDto> getAwardListByIdAndStatus(Integer id, Integer status) {
        return awardMapper.getAwardListByIdAndStatus(id, status);
    }

    @Override
    public Integer getLastAwardIdSendInform(String awardId) {
        Integer defaultAwardId = 0;

        try {
            if (StringUtils.isEmpty(awardId) || !NumberUtil.isNumeric(awardId)) {
                logger.error("getLastAwardIdSendInform failed");
                return defaultAwardId;
            }
            return Integer.parseInt(awardId);
        } catch (Exception e) {
            logger.error("getLastAwardIdSendInform failed", e);
            return defaultAwardId;
        }

    }

    @Override
    public String getAwardCloseToTimeoutMsg() {

        String msg = commonConfigService.getValue(ConfigKeys.AWARD_CLOSE_TO_TIMEOUT_MSG);

        try {
            if (StringUtils.isEmpty(msg)) {
                logger.error("get award close to timeout msg failed");
                return null;
            }
            return msg;
        } catch (Exception e) {
            logger.error("get award close to timeout msg failed", e);
            return null;
        }

    }

    @Override
    public Integer getAwardExpiredRemainingTimeUnitHour() {

        Integer defaultTime = 1;

        String remainingTime = commonConfigService.getValue(ConfigKeys.AWARD_EXPIRED_RAMAINING_TIME);

        try {
            if (StringUtils.isEmpty(remainingTime) || !NumberUtil.isNumeric(remainingTime)) {
                logger.error("getAwardExpiredRemainingTimeUnitHour failed");
                return defaultTime;
            }
            return Integer.parseInt(remainingTime);
        } catch (Exception e) {
            logger.error("getAwardExpiredRemainingTimeUnitHour failed", e);
            return defaultTime;
        }

    }

    @Override
    public Integer currentRoundAmount(Integer userId,Integer roundCount) {
        return awardMapper.currentRoundAmount(userId,roundCount);
    }
}
